package com.cy.service.impl;

import com.cy.entity.Address;
import com.cy.mapper.AddressMapper;
import com.cy.service.IAddressService;

import com.cy.service.IDistrictService;
import com.cy.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IAddressServiceImpl implements IAddressService {

    @Autowired
    public AddressMapper addressMapper;
    //添加用户收获地址的业务层依赖这个接口
    @Autowired
    private IDistrictService districtService;

    @Value("${user.address.max-count}")
    private Integer maxCount;

    @Override
    public void addNewAddress(Integer uid, String username, Address address) {
        //1判读是否达到用户上限
        Integer count = addressMapper.countByUid(uid);
        if(count>=maxCount){
            throw new AddressCountLimitException("收获地址超出上限");
        }
        //对address对象补全省市区
        String provinceName = districtService.getNameByCode(address.getProvinceCode());
        String cityName = districtService.getNameByCode(address.getCityCode());
        String areaName = districtService.getNameByCode(address.getAreaCode());

        address.setProvinceName(provinceName);
        address.setCityName(cityName);
        address.setAreaName(areaName);


        //uid is_delete modified_user
        address.setUid(uid);
        //count==0 1 else 0
        Integer isDefault=count==0?1:0;
        address.setIsDefault(isDefault);

        address.setCreatedUser(username);
        address.setModifiedUser(username);
        address.setCreatedTime(new Date());
        address.setModifiedTime(new Date());
//拆入收货地址
        Integer rows = addressMapper.insert(address);
        if(rows!=1)
        {
            throw new InsertException("插入用户的地址产生未知异常");
        }
    }

    @Override
    public List<Address> getByUid(Integer uid) {
        List<Address> list= addressMapper.findByUid(uid);
        for(Address address:list)
        {
            //address.setAid(null);
            //address.setUid(null);
            address.setProvinceCode(null);
            address.setCityCode(null);
            address.setAreaCode(null);
            address.setTel(null);
            address.setCreatedTime(null);
            address.setCreatedUser(null);
            address.setModifiedTime(null);
            address.setModifiedUser(null);

        }
        return list;
    }

    @Override
    public void setDefault(Integer aid, Integer uid, String username) {
        Address result= addressMapper.findByAid(aid);
        if(result==null)
        {
            throw new AddressNotFoundException("收获地址找不到");
        }
        //检测获取到的收货地址数据归属
        if(!result.getUid().equals(uid)){
            throw new AccessDeniedException("非法访问");
        }

        Integer rows = addressMapper.updateNoDefault(uid);
        if(rows<1){
            throw new UpdateException("更新出了错误");
        }

        Integer row = addressMapper.updateDefaultByAid(aid, username, new Date());
        if(row!=1){
            throw new UpdateException("更新产生异常");
        }

    }

    @Override
    public void delete(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        if(result==null){
            throw new AddressNotFoundException("地址无法找到");
        }

        if(!result.getUid().equals(uid))
        {
            throw new AccessDeniedException("非法数据访问");
        }


        Integer rows = addressMapper.deleteByAid(aid);
        if(rows!=1){
            throw new DeleteException("删除数据产生未知异常");
        }
        Integer count = addressMapper.countByUid(uid);

        if(count==0) return;

        if(result.getIsDefault()==0){
           return;
        }
        Address address = addressMapper.findLastModified(uid);
        rows=addressMapper.updateDefaultByAid(address.getAid(),username,new Date());

        if(rows!=1){
            throw new UpdateException("更新时产生数据异常");
        }

    }
}
